package bg.code.commons.builder.utils;

/**
 * @author 郑兴泉 956607644@qq.com
 * @data 2020/3/18
 * 描述：
 */
public class Strings {

    /**
     * 如果此字符串为 null 或者全为空白字符，则返回 true
     *
     * @param cs 字符串
     * @return 如果此字符串为 null 或者全为空白字符，则返回 true
     */
    public static boolean isBlank(CharSequence cs) {
        if (null == cs) {
            return true;
        }
        int length = cs.length();
        for (int i = 0; i < length; i++) {
            if (!(Character.isWhitespace(cs.charAt(i)))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 如果此字符串为 null 或者全为空白字符，则返回 true
     *
     * @param cs 字符串
     * @return 如果此字符串为 null 或者全为空白字符，则返回 true
     */
    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    public static String capitalize(String string) {
        String first = string.substring(0, 1);
        String end = string.substring(1);
        return first.toUpperCase() + end;
    }

    public static String uncapitalize(String string) {
        String first = string.substring(0, 1);
        String end = string.substring(1);
        return first.toLowerCase() + end;
    }

    public static <T> String join(String sp, T... array) {
        return concat(sp, array).toString();
    }

    public static <T> StringBuilder concat(Object c, T[] objs) {
        StringBuilder sb = new StringBuilder();
        if (null != objs && 0 != objs.length) {
            sb.append(objs[0]);

            for (int i = 1; i < objs.length; ++i) {
                sb.append(c).append(objs[i]);
            }

            return sb;
        } else {
            return sb;
        }
    }

    public static String sNull(Object o) {
        return sNull(o, "");
    }

    public static String sNull(Object o, String defaultValue) {
        if (o == null) {
            return defaultValue;
        }
        return o.toString();
    }

    public static boolean sBoolean(Object o) {
        String b = sNull(o, "");
        if (isNotBlank(b)) {
            return Boolean.parseBoolean(b);
        }
        return false;
    }

    public static int sInt(Object o) {
        String b = sNull(o, "");
        if (isNotBlank(b)) {
            return Integer.parseInt(b);
        }
        return 0;
    }
}
